'use strict';
const uniID = require('uni-id-common');

exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	
	// 初始化uni-id-common实例
	// 修复bug：检查context.CLIENTAPPS是否存在且不为空
	const clientInfo = context.CLIENTAPPS && context.CLIENTAPPS.length > 0 ? context.CLIENTAPPS[0] : {};
	const uniIdCommon = uniID.createInstance({
		clientInfo: clientInfo
	})
	
	// 根据event中的action参数执行不同操作
	const { action, params } = event;
	
	switch(action) {
		case 'register':
			return await register(uniIdCommon, params);
		case 'login':
			return await login(uniIdCommon, params);
		case 'getUserInfo':
			return await getUserInfo(uniIdCommon, params);
		case 'getExamList':
			return await getExamList(params);
		case 'getExamStatistics':
			return await getExamStatistics();
		case 'registerForExam':
			return await registerForExam(params);
		default:
			return {
				errCode: 400,
				message: '未知的操作'
			}
	}
};

// 注册功能
async function register(uniIdCommon, params) {
	try {
		const res = await uniIdCommon.register(params);
		return res;
	} catch (error) {
		return {
			errCode: 500,
			message: '注册失败: ' + error.message
		}
	}
}

// 登录功能
async function login(uniIdCommon, params) {
	try {
		const res = await uniIdCommon.login(params);
		return res;
	} catch (error) {
		return {
			errCode: 500,
			message: '登录失败: ' + error.message
		}
	}
}

// 获取用户信息功能
async function getUserInfo(uniIdCommon, params) {
	try {
		const res = await uniIdCommon.getUserInfo(params);
		return res;
	} catch (error) {
		return {
			errCode: 500,
			message: '获取用户信息失败: ' + error.message
		}
	}
}

// 获取考试列表
async function getExamList(params) {
	try {
		// 这里应该从数据库查询考试列表
		// 模拟数据
		const examList = {
			rows: [
				{
					id: '1',
					title: '高等数学 - 期中考试',
					description: '高等数学期中考试',
					startTime: '2023-06-01 09:00:00',
					endTime: '2023-06-01 11:00:00',
					duration: 120,
					totalScore: 100,
					passScore: 60,
					status: 1, // 1:未开始 2:进行中 3:已结束
					examType: 1, // 1:正式考试 2:模拟考试
					questionCount: 50,
					participantCount: 120,
					createTime: '2023-05-01 10:00:00'
				},
				{
					id: '2',
					title: '大学英语 - 期末考试',
					description: '大学英语期末考试',
					startTime: '2023-06-15 14:00:00',
					endTime: '2023-06-15 16:00:00',
					duration: 120,
					totalScore: 100,
					passScore: 60,
					status: 1,
					examType: 1,
					questionCount: 40,
					participantCount: 95,
					createTime: '2023-05-15 10:00:00'
				}
			],
			total: 2,
			page: 1,
			pageSize: 10
		};
		
		return {
			errCode: 0,
			data: examList
		};
	} catch (error) {
		return {
			errCode: 500,
			message: '获取考试列表失败: ' + error.message
		};
	}
}

// 获取考试统计信息
async function getExamStatistics() {
	try {
		// 模拟数据
		const statistics = {
			totalExams: 20,
			upcomingExams: 5,
			ongoingExams: 2,
			completedExams: 13,
			totalParticipants: 450,
			averageScore: 78.5
		};
		
		return {
			errCode: 0,
			data: statistics
		};
	} catch (error) {
		return {
			errCode: 500,
			message: '获取考试统计信息失败: ' + error.message
		};
	}
}

// 报名考试
async function registerForExam(params) {
	try {
		// 检查参数
		if (!params || !params.examId) {
			return {
				errCode: 400,
				message: '缺少考试ID参数'
			};
		}
		
		// 这里应该处理考试报名逻辑
		// 模拟数据
		return {
			errCode: 0,
			data: true
		};
	} catch (error) {
		return {
			errCode: 500,
			message: '报名考试失败: ' + error.message
		};
	}
}